{extend name="Base" /}
{block name="title"}{$title}{/block}
{block name="body"}
{load href="/tinymce/tinymce.min.js"}
<script type='text/javascript' src='/js/backend/custom.js?{:VER}'></script>
<div class="layui-tab layui-tab-brief" lay-filter="tab">
	<ul class="layui-tab-title">
		<li class="layui-this">{$name1}</li>
		<li lay-id="1">{$name2}</li>
		<li>{$name3}</li>
	</ul>
	<div class="layui-tab-content">
		<div class="layui-tab-item layui-show">
			<button type="button" class="layui-btn" data-type="msg_alldel">批量删除</button>
			<table id="msg" lay-filter="msg" class="layui-hide"></table>
			<script type="text/html" id="msg_bar">
				<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="re">回复</a>
			</script>
		</div>

		<div class="layui-tab-item">
			<button type="button" class="layui-btn" data-type="announcement_add">发布公告</button>
			<table id="announcement" lay-filter="announcement" class="layui-hide"></table>
			<script type="text/html" id="announcement_bar">
				<a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
				<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
			</script>
		</div>

		<div class="layui-tab-item">
			<form class="layui-form">
				<div class="layui-form-item layui-form-text">
					<textarea id="notice" name="notice" placeholder="请输入提醒内容" class="layui-textarea">{$notice.notice}</textarea>
				</div>

				<div class="layui-form-item">
					<div class="layui-input-inline">
						<button class="layui-btn" lay-submit="" lay-filter="submit">提交</button>
					</div>
				</div>
			</form>
		</div>
	</div>
</div>

<script>
	layui.use(['table', 'element', 'layer', 'form'], function () {
		var table = layui.table;
		var element = layui.element;
		var layer = layui.layer;
		var form = layui.form;

		//监听提交
		form.on('submit(submit)', function (data) {
			send(data.field, "users", "doLimit");
			return false;
		});

		initEdit("notice", 400);

		var active = {
			msg_alldel: function () {
				var checkStatus = table.checkStatus('msg'), data = checkStatus.data, id = [];
				for (var i = 0; i < data.length; i++) {
					id[i] = data[i].id;
				}
				send({id: id.join()}, "system", "delMessage");
				table.reload('msg');
			},
			announcement_add: function () {
				layer.open({
					type: 1,
					title: "发布公告",
					closeBtn: 1,
					area: '950px;',
					shade: 0.8,
					id: 'announcement_add',
					btn: ['提交', '取消'],
					btnAlign: 'c',
					content: "<div style='padding: 15px'>" +
					"<textarea id=\"ment\" name=\"ment\" placeholder=\"请输入公告内容\" class=\"layui-textarea\"></textarea>" +
					"</div>",
					yes: function (index) {
						var content = {content: tinymce.get('ment').getContent({format: 'raw'})};
						send(content, "system", "releaseAnnouncement");
						table.reload('announcement');
						layer.close(index);
					}
				});
				tinymce.remove('#ment');
				initEdit("ment", 200);
			}
		};

		$('.layui-tab-item .layui-btn').on('click', function () {
			var type = $(this).data('type');
			active[type] ? active[type].call(this) : '';
		});

		table.render({
			id: "msg",
			elem: '#msg',
			page: true,
			height: 650,
			even: true,
			method: 'post',
			where: {
				type: 2
			},
			url: "/backend/system/getMessage",
			cols: [[
				{checkbox: true, fixed: true},
				{field: 'id', title: 'ID', width: 80},
				{field: 'from', title: '发信人', width: 150},
				{field: 'time', title: '发送时间', width: 180},
				{field: 'content', title: '信息内容', width: 500},
				{field: 'status', title: '状态', width: 80},
				{field: 'executor', title: '处理者', width: 150},

				{title: '操作', width: 80, align: 'center', toolbar: '#msg_bar', fixed: 'right'}
			]]
		});

		element.on('tab(tab)', function (data) {
			if (Number(data.index) === 1) {
				table.render({
					id: "announcement",
					elem: '#announcement',
					page: true,
					height: 650,
					even: true,
					method: 'post',
					where: {
						type: 2
					},
					url: "/backend/system/getAnnouncement",
					cols: [[
						{space: true},
						{field: 'id', title: 'ID', width: 80},
						{field: 'content', title: '信息内容', width: 500},

						{title: '操作', width: 150, align: 'center', toolbar: '#announcement_bar', fixed: 'right'}
					]]
				});
			}
		});

		//监听工具条
		table.on('tool(msg)', function (obj) {
			var data = obj.data;
			if (obj.event === 're') {
				layer.prompt({
						formType: 2,
						title: '回复 ' + data.from,
						value: ""
					},
					function (value, index) {
						layer.close(index);
						var d = {
							msg: value,
							from: data.receiver,
							to: data.sender,
							uuid: data.uuid
						};
						$.ajax({
							type: "POST",
							url: "/index/message/send",
							cache: false,
							data: d,
							dataType: "json",
							success: function (e) {
								if (e.status === 1) {
									obj.update({
										status: "已处理",
										executor: data.to
									});
									layer.alert(e.message, {
										//成功图标
										icon: 1
									});
								} else if (e.status === 0) {
									layer.alert(e.message, {
										//失败图标
										icon: 2
									});
								} else {
									layer.alert("系统错误，请稍后重试", {
										//失败图标
										icon: 2
									});
								}
							}
						});
					}
				);
			}
		});

		//监听工具条
		table.on('tool(announcement)', function (obj) {
			var data = obj.data;
			if (obj.event === 'edit') {
				layer.open({
					type: 1,
					title: "修改公告",
					closeBtn: 1,
					area: '950px;',
					shade: 0.8,
					id: 'announcement_edit',
					btn: ['提交', '取消'],
					btnAlign: 'c',
					content: "<div style='padding: 15px'>" +
					"<textarea id=\"editment\" name=\"editment\" placeholder=\"请输入公告内容\" class=\"layui-textarea\"></textarea>" +
					"</div>",
					yes: function (index) {
						var content = tinymce.get('editment').getContent({format: 'raw'});
						send({content: content, type: 1, id: data.id}, "system", "releaseAnnouncement");
						table.reload('announcement');
						layer.close(index);
					}
				});
				tinymce.remove('#editment');
				initEdit("editment", 200);
				tinymce.get('editment').setContent(data.content);
			} else if (obj.event === 'del') {
				layer.confirm('确认要删除么？', function (index) {
					obj.del();
					layer.close(index);
					$.ajax({
						type: "POST",
						url: "/backend/system/delAnnouncement",
						cache: false,
						data: {
							id: data.id
						},
						dataType: "json",
						success: function (e) {
							if (e.status === 1) {
								layer.alert(e.message, {
									//成功图标
									icon: 1
								});
							} else if (e.status === 0) {
								layer.alert(e.message, {
									//失败图标
									icon: 2
								});
							} else {
								layer.alert("系统错误，请稍后重试", {
									//失败图标
									icon: 2
								});
							}
						}
					})
				});
			}
		});
	});

	function initEdit(id, height) {
		tinymce.init({
			language: "zh_CN",
			selector: '#' + id,
			height: height,
			theme: 'modern',
			branding: false,
			plugins: [
				'advlist autolink lists link image charmap print preview hr anchor pagebreak',
				'searchreplace wordcount visualblocks visualchars code fullscreen',
				'insertdatetime media nonbreaking save table contextmenu directionality',
				'emoticons paste textcolor colorpicker textpattern imagetools codesample toc localautosave'
			],
			toolbar: 'undo redo | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image media preview | forecolor backcolor | codesample | localautosave',
			image_advtab: false,//图片高级编辑，比如边距等，建议关闭
			setup: function (editor) {
				editor.addMenuItem('imgbed', {
					text: 'ClanTu图床',
					icon: "mce-ico mce-i-image",
					context: 'tools',
					onclick: function () {
						window.open("http://www.clantu.com/register/i/dAszQ1");
					}
				});
			}
		});
	}
</script>
{/block}